<?php

/**
 * This is the model class for table "surat_masuk".
 *
 * The followings are the available columns in table 'surat_masuk':
 * @property string $id_surat_masuk
 * @property string $id_instansi
 * @property string $no_surat
 * @property string $upload_file
 * @property string $info_penyimpanan
 * @property string $tanggal_masuk
 * @property string $id_pegawai
 * @property string $tanggal_diterima
 * @property string $keterangan
 *
 * The followings are the available model relations:
 * @property MstInstansi $idInstansi
 * @property MstPegawai $idPegawai
 */
class SuratMasuk extends CActiveRecord
{
        public $untuk;
        public $dari;

    /**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return SuratMasuk the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'surat_masuk';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('tanggal_masuk, id_instansi, upload_file, info_penyimpanan, id_pegawai', 'required'),
			array('id_instansi, id_pegawai', 'length', 'max'=>20),
			array('no_surat', 'length', 'max'=>45),
			array('tanggal_masuk', 'date', 'format'=>'yyyy-MM-dd'),
                        array('upload_file', 'file', 'on'=>'create', 'types'=>'jpg, gif, png, pdf, doc, docx'),
			array('tanggal_masuk, tanggal_diterima, keterangan', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
                            'id_surat_masuk, id_instansi, no_surat, 
                            upload_file, info_penyimpanan, tanggal_masuk, 
                            id_pegawai, tanggal_diterima, keterangan,
                            untuk, dari',
                            'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'Instansi' => array(self::BELONGS_TO, 'Instansi', 'id_instansi'),
			'Pegawai' => array(self::BELONGS_TO, 'Pegawai', 'id_pegawai'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_surat_masuk' => 'Id Surat Masuk',
			'id_instansi' => 'Nama Pengirim',
			'no_surat' => 'No Surat',
			'upload_file' => 'Upload File',
			'info_penyimpanan' => 'Info Penyimpanan',
			'tanggal_masuk' => 'Tanggal Masuk Surat',
			'id_pegawai' => 'Nama Penerima',
			'tanggal_diterima' => 'Tanggal Diterima',
			'keterangan' => 'Keterangan',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($by=null)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_surat_masuk',$this->id_surat_masuk,true);
		$criteria->compare('id_instansi',$this->id_instansi,true);
		$criteria->compare('no_surat',$this->no_surat,true);
		$criteria->compare('upload_file',$this->upload_file,true);
		$criteria->compare('info_penyimpanan',$this->info_penyimpanan,true);
		$criteria->compare('tanggal_masuk',$this->tanggal_masuk,true);
		$criteria->compare('tanggal_diterima',$this->tanggal_diterima,true);
		$criteria->compare('keterangan',$this->keterangan,true);
                $criteria->compare('Instansi.nama', $this->dari, true);
                $criteria->compare('Pegawai.nama', $this->untuk, true);
                
                if($by=='user')
                {
                    $criteria->compare('t.id_pegawai', Yii::app()->user->id, false);
                }
		
                $criteria->order = 'id_surat_masuk DESC';
                
                $criteria->with = array( 'Instansi', 'Pegawai' );
                
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}